/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.thu.laud.objectstore.query;

import java.nio.ByteBuffer;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.UUID;

import org.apache.cassandra.thrift.Cassandra;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.query.expression.Expression;
import cn.edu.thu.laud.objectstore.CassandraConsistency;
import cn.edu.thu.laud.objectstore.index.IndexContainer;
import cn.edu.thu.laud.objectstore.index.IndexHandler;
import cn.edu.thu.laud.objectstore.index.IndexMeta;
import cn.edu.thu.laud.objectstore.index.KVColumn;

/**
 * Representation of a boolean expression in Cassandra queries.
 * @author zhuoan
 */
public class CassandraBooleanExpression extends CassandraExpression
{
    Set<KVColumn> keys;

    /**
     * Constructor when the expression represents a comparison, between the field and a value.
     * @param familyName Family name
     * @param columnName Column name
     * @param value The value
     * @param op The operator (eq, noteq, lt, gt, etc)
     */
    public CassandraBooleanExpression(Cassandra.Client client, String colName, Object value, Expression.Operator op, int count, ByteBuffer disColName, Set<ByteBuffer> disColValues, IndexContainer container, IndexMeta idxMeta)
    {
    	if (value instanceof String || value instanceof Number || value instanceof UUID || value instanceof ByteBuffer)
        {
    		try {
    			
    			Object startValue = null;
    			Object endValue = null;
    			boolean incStart = false;
    			boolean incEnd = false;
    			
    			if (op == Expression.OP_EQ)
    	        {
    				startValue = value;
    				endValue = value;
    				incStart = true;
    				incEnd = true;
    	        }
    	        else if (op == Expression.OP_GT)
    	        {
    	        	startValue = value;
    	        }
    	        else if (op == Expression.OP_GTEQ)
    	        {
    	        	startValue = value;
    				incStart = true;
    	        }
    	        else if (op == Expression.OP_LT)
    	        {
    				endValue = value;
    	        }
    	        else if (op == Expression.OP_LTEQ)
    	        {
    				endValue = value;
    				incEnd = true;
    	        }
    			
    			keys = IndexHandler.searchContainer(client, colName, startValue, incStart, endValue,
						incEnd, null, count, false, false, null, disColName, disColValues, container, idxMeta, CassandraConsistency.get());
    			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new NucleusException(e.getMessage(), e);
			}
        }
        else
        {
            throw new NucleusException("Don't currently support use of Literal of type "+value.getClass().getName() + " in expressions");
        }
    }

    public CassandraBooleanExpression(CassandraBooleanExpression expr1, CassandraBooleanExpression expr2, Expression.Operator op)
    {
        if (op == Expression.OP_AND)
        {
            keys = new LinkedHashSet<KVColumn>(expr1.getKeys());
            keys.retainAll(expr2.getKeys());
        }
        else if (op == Expression.OP_OR)
        {
            keys = new LinkedHashSet<KVColumn>(expr1.getKeys());
            keys.addAll(expr2.getKeys());
        }
        else
        {
            throw new NucleusException("Dont currently support operator "+op + " in boolean expression for Cassanra");
        }
    }

    public Set<KVColumn> getKeys()
    {
        return keys;
    }

}